﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Core;
using System.IO;

namespace SisocanaFronEnd.Controllers
{
    public class UtilController : SiteController
    {
     
       private IPersonaService _personaService;

        public UtilController()
            : this(new PersonaService())
        {
        }

        public UtilController(IPersonaService personaService)
        {
            _personaService = personaService;
        }

        public ActionResult test()
        {
            return View();
        }


        
      public ActionResult ACJson(string q, int limit){
         
          int idSitio = ViewBag.Site.id;          
          var result = _personaService.SearchPersonACJSON(idSitio, q.Trim(),limit);
          List<jsonResp> tags = new List<jsonResp>();

          foreach (PersonaDesaparecidaModel p in result)
          {
              jsonResp pR = new jsonResp();
              pR.Name = p.nombre.Trim();
              
              pR.Surname = p.apellido.Trim();
              //pR.ImgUrl = "http://localhost/SisocanaFronEnd/Site/imagen/1";

              pR.ImgUrl = Url.Action("imagenT", new { id = p.id });
              pR.id = p.id;
              pR.idsitio = p.idSitio;
              tags.Add(pR);
          }
           return Json(tags,JsonRequestBehavior.AllowGet);
    
      }

      [HttpPost]
      public ActionResult ACJsonR(string id, string idsitio, string s)
      {

          if (idsitio == "" || id == "")
          {
              return RedirectToAction("SearchPersonResults", "SearchPerson",new{Keyword=s});
          }
          else
          {
              return  RedirectToAction("FoundPerson","Person", new { id = id,
                                                                          externalEntity = false,
                                                                          searchWSId = 0,
                                                                          idSitio = idsitio,
                                                                          name ="",
                                                                          lastName="",
                                                                          date = new DateTime(),
                                                                          phone= ""
                                                                         });             
          }

      }


      public void ImagenT(int id)
      {
          byte[] imageData = null;
          MemoryStream ms = null;
          System.Drawing.Image fullsizeImage = null;
          String imageID = null;

          try
          {
              DataImagen per = _personaService.GetPhoto(id);
              
              //SI LA IMAGEN EN NULL VAMOS A MOSTRAR UNA POR DEFECTO...
              if (per != null && per.imgByte != null)
              {
                  imageData = per.imgByte;
                  ms = new MemoryStream(imageData);
                  fullsizeImage = System.Drawing.Image.FromStream(ms);

                  Response.ContentType = "image/Jpeg";
                  ImageResize ir = new ImageResize();

                  // Load your image and perform any resizing here

                  ir.File = fullsizeImage;
                  ir.Height = 40;
                  ir.Width = 40;
                  //get the thumbnail

                  ir.GetThumbnail().Save(Response.OutputStream,
                      System.Drawing.Imaging.ImageFormat.Jpeg);

              }
              else
              {
                  
                  string file = Server.MapPath("~/Content/images/no-photo.gif");
                  System.Drawing.Image image = System.Drawing.Image.FromFile(file);                 
                  System.Drawing.Image thumbnailImage = image.GetThumbnailImage(40, 40, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                  ms = new MemoryStream();
                  thumbnailImage.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
                  byte[] imageContent = new Byte[ms.Length];
                  ms.Position = 0;
                  ms.Read(imageContent, 0, (int)ms.Length);
                  Response.ContentType = "image/jpeg";
                  Response.BinaryWrite(imageContent);

              }

          }
          catch (Exception ex)
          {
              //chanchada
              string file = Server.MapPath("~/Content/images/no-photo.gif");
              System.Drawing.Image image = System.Drawing.Image.FromFile(file);
              System.Drawing.Image thumbnailImage = image.GetThumbnailImage(40, 40, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
              ms = new MemoryStream();
              thumbnailImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
              byte[] imageContent = new Byte[ms.Length];
              ms.Position = 0;
              ms.Read(imageContent, 0, (int)ms.Length);
              Response.ContentType = "image/jpeg";
              Response.BinaryWrite(imageContent);
          }
          finally
          {
              ms.Close();
          }

      }


      public bool ThumbnailCallback()
      {
          return true;
      }


    }



    public class jsonResp
    {

        public string Name { get; set; }
        public string Surname { get; set; }
        public string ImgUrl { get; set; }
        public int id { get; set; }
        public int idsitio { get; set; }

    }

    
}
